package com.qf.config;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qf.job.MyJob;
import com.qf.job.MyJob2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticJobConfig {
    @Autowired
    MyJob myJob;
    @Autowired
    MyJob2 myJob2;
    @Autowired
    ZookeeperRegistryCenter registryCenter;
    @Value("${myjob.count}")
    private int shardingCount;
    @Value("${myjob.cron1}")
    private String cron1;

    @Value("${myjob.cron2}")
    private String cron2;
    /**
     * 配置任务详细信息
     * @param jobClass 任务执行类
     * @param cron 执行策略
     * @param shardingTotalCount 分片数量
     * @return
     */
    private LiteJobConfiguration createJobConfiguration(final Class<? extends SimpleJob> jobClass,
                                                        final String cron,
                                                        final int shardingTotalCount){
        /*
        Core对应JobCoreConfiguration，用于提供作业核心配置信息，如：作业名称、分片总数、CRON表达式等。
        Type对应JobTypeConfiguration，有三个子类分别对应SIMPLE, DATAFLOW和SCRIPT类型作业，供3种作业需要的不同配置，
            如：DATAFLOW类型是否流式处理或SCRIPT类型的命令行等。
        Root对应JobRootConfiguration，有两个子类分别对应Lite和Cloud部署类型，提供不同部署类型所需的配置，
            如：Lite类型的是否需要覆盖本地配置或Cloud占用CPU或Memory数量等。
         */

        //创建JobCoreConfigurationBuilder
        JobCoreConfiguration.Builder JobCoreConfigurationBuilder = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount);
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfigurationBuilder.build();

        //创建SimpleJobConfiguration
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName());

        //创建LiteJobConfiguration
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration)
                        .jobShardingStrategyClass("com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy")
                        .overwrite(true)
                        .build();

        return liteJobConfiguration;
    }

    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob() {
        //创建SpringJobScheduler
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(myJob, registryCenter,
                createJobConfiguration(myJob.getClass(), cron1, shardingCount));

        return springJobScheduler;
    }

    /**
     * 如果需要执行不同频度的任务调度，只需要再添加一个调度器即可
     * @return
     */
    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob2() {
        //创建SpringJobScheduler
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(myJob2, registryCenter,
                createJobConfiguration(myJob2.getClass(), cron2, shardingCount));

        return springJobScheduler;
    }
}
